<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :title="title"
    align-center
    draggable
    width="40%"
    style="border-radius: 10px; padding: 5px 10px"
  >
    <el-form :model="form" label-width="120px" :rules="rules">
		<el-form-item label="条目类型">
		  <el-input v-model="form.type" style="width: 380px" />
		</el-form-item>
      <el-form-item label="条目名称">
        <el-input v-model="form.target" style="width: 380px" />
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch v-model="form.state" />
      </el-form-item>
      <el-form-item label="备注说明" class="ins">
        <el-input v-model="form.ins" type="textarea" style="width: 380px" class="ins"/>
      </el-form-item>
      <el-form-item>
        <el-button style="width: 150px" type="primary" @click="submitForm"
          >确定</el-button
        >
        <el-button style="width: 150px" @click="onClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import {
  ref,
  onBeforeMount,
  reactive,
  defineProps,
  defineEmits,
  watch,
} from "vue";
import { addRule, updatetRule } from "@/api/score";
import { ElMessage } from "element-plus";


const form = reactive({
  id:"",
  target: "",
  type:"",
  state: true,
  ins: "",
});
watch(props, (newValue, oldValue) => {
  if (newValue.selectContent != undefined) {
    form.id = newValue.selectContent.id;
    form.target = newValue.selectContent.target;
    form.state = newValue.selectContent.state == 1 ? true : false;
    form.ins = newValue.selectContent.ins;
  }
});

const dialogFormVisible = ref(true);
const props = defineProps({
  dialogFormVisible: {
    type: Boolean,
    required: true,
  },
  close: {
    type: Function,
    default: () => {},
  },
  update: {
    type: Function,
    default: () => {},
  },
  selectContent: {
    type: Object,
    required: true,
  },
  type: {
    type: String,
    required: true,
  },
  title: {
    type: String,
    required: true,
  },
});

const onClose = () => {
  props.close();
};

const submitForm = () => {
  if (props.type == "add") {
    addRule(form).then((data) => {
      if (data.code == 200) {
        ElMessage({
          message: "新增成功",
          type: "success",
        });
        props.close();
        props.update();
      } 
      else {
        ElMessage.error("新增失败");
      }
    });
  } 
  else if (props.type == "edit") {
    updatetRule(form).then((data) => {
      if (data.code == 200) {
        ElMessage({
          message: "编辑成功",
          type: "success",
        });
        props.close();
        props.update();
      } 
      else {
        ElMessage.error("编辑失败");
      }
    });
  }
};
</script>
<style lang="scss" scoped >
::v-deep .el-form-item__label:before {
  content: "* ";
  color: red;
}
.ins {
  ::v-deep .el-form-item__label:before {
    content: " ";
  }
}
</style>